from lib.adb import Physical, Emulator, DockerEmulator
from lib.model.database.Database import Database
from lib.modules import ModuleGeneral
from lib.analysis import Core
from config import config

import argparse
import logging
import coloredlogs

logging.basicConfig(level=config['GLOBAL'].get('logging_level'))
logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG', logger=logger)


def main(args) -> None:
    """
    Function that create the process
    :param args:
    :return:
    """

    Database.set_configuration(config)
    device = None
    device_type = config['GLOBAL'].get('device_type')
    if  device_type == "physical":
        device = Physical.Physical(config['PATH'], config['PHYSICAL'])
    elif device_type == "emulator":
        device = Emulator.Emulator(config['PATH'], config)
    elif device_type == "docker":
        device = DockerEmulator.DockerEmulator(config['PATH'],config['DOCKER'])
    else:
        logging.error("You must choose a device type (physical, emulator, docker)")
    logging.info("Launching device")
    device.start()
    logging.info(f"Architecture of the device is : {device.get_device_arch()} ")
    core = Core.Core(config, device, ModuleGeneral.ModuleGeneral, args.path)
    core.start_analysis()
    device.kill_emulators()


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Some shitty sandbox for Android malwares')
    parser.add_argument('path', metavar='file(s)', type=str, help='directory or file to process')
    args = parser.parse_args()

    main(args)
